﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using A5Lib;
using System.Xml.XPath;
using System.Xml;
using System.IO;
using System.Web.Hosting;

namespace A5Site
{
    public partial class UserApplianceControl : System.Web.UI.UserControl
    {
        

        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                
            }
            deleteTimeError.Text = "";
            addTimeError.Text = "";
        }
       
        public void setAppliance(Appliance app)
        {
            applianceNameLabel.Text = app.Name;
            foreach (DateTimePair dtp in app.ScheduleTimes.Dates)
            {
                timeListBox.Items.Add(dtp.d1.TimeOfDay + " to " + dtp.d2.TimeOfDay);
            }
        }
        protected void deleteSelectedTimeButton_Click(object sender, EventArgs e)
        {
            if (timeListBox.SelectedIndex < 0)
            {
                deleteTimeError.Text = "No Time Selected.";
                return;
            }
            try
            {
                UserServices.UserServicesClient usc = new UserServices.UserServicesClient();
                A5Lib.User u = (A5Lib.User)Session["myuser"];
                Appliance app = u.getApplianceByName(applianceNameLabel.Text);
                app.ScheduleTimes.Dates.RemoveAt(timeListBox.SelectedIndex);
                timeListBox.Items.RemoveAt(timeListBox.SelectedIndex);
                if(usc.WriteUser(u, false))
                    deleteTimeError.Text = "Deleted Time Successfully";
                else{
                    deleteTimeError.Text = "Delete Time Failed";
                }
            }
            catch (Exception ex)
            {
                addTimeButton.Text = ex.Message;
                Response.Redirect("Login.aspx");
            }
        }

        protected void addTimeButton_Click(object sender, EventArgs e)
        {
            try
            {
                UserServices.UserServicesClient usc = new UserServices.UserServicesClient();
                A5Lib.User u = (A5Lib.User)Session["myuser"];
                DateTime dt1, dt2;
                dt1 = TimeRangePicker1.getStartTime();
                dt2 = TimeRangePicker1.getEndTime();

                Appliance app = u.getApplianceByName(applianceNameLabel.Text);
                if (app.ScheduleTimes.addDateTimePair(dt1, dt2))
                {
                    timeListBox.Items.Add(dt1.TimeOfDay + " - " + dt2.TimeOfDay);
                    addTimeError.Text = "Time Range Added Successfully";        
                    usc.WriteUser(u, false);
                }
                else
                {
                    addTimeError.Text = "Time Range Add Fail - Ensure no overlap with other time ranges.";
                }
            }
            catch (Exception ex)
            {
                addTimeButton.Text = ex.Message;
                Response.Redirect("Login.aspx");
            }
            
        }
    }
}